<?php

namespace App\Http\Resources\Order;

use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;

class AppOrder extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        $order['contract_no'] = $this->contract_no;
        $order['order_no'] = $this->order_no;
        $order['id'] = $this->id;
        $user = $this->user;
        $order['user_name'] = $user->name;
        $order['user_phone_num'] = $user->contact_manner;
        $order_money = $this->order_money;
        $order['order_money'] = $order_money;
        $bank_card = $this->bank_card;
        $order['bank_sum_name'] = $bank_card->bank_sum_name;
        $order['bank_num'] = $bank_card->bank_num;
        $product = $this->product;
        $time_unit = $product->time_unit;
        $long_time = $product->long_time;
        $pay_long = $product->pay_long;
        $pay_time_unit = $product->pay_time_unit;
        $order['product_name'] = $product->name;
        $product_rate = $product->rate;
        $order['product_rate'] = $product_rate;
        $order['results_money'] = (int)($this->results_money);
        $current = Carbon::now();
        $valid_time = $this->valid_time;
        $valid_day = $current->diffInDays($valid_time, false);
        $valid_day = $valid_day < 0 ? 0 - $valid_day : '未生效';
        $order['valid_day']  =  $valid_day . '天';
        $order['valid_time'] = date('Y-m-d', strtotime($valid_time));
        $order['sign_time'] = date('Y-m-d', strtotime($this->sign_time));
        $order['account_time'] = date('Y-m-d', strtotime($this->account_time));
        $contract_end_date = $this->contract_end_date;
        $order['contract_end_date'] =  date('Y-m-d', strtotime($contract_end_date));
        $end_day = $current->diffInDays($contract_end_date, false);
        $end_day = $end_day > 0 ? $end_day : '已到期';
        $order['end_day']  =  $end_day . '天';
        $order['day_money'] = round($order_money * $product_rate / 365 / 100, 2) . "元";
        $order['already_money'] = $this->interest_records->where('status', '3')->sum('interest');
        $order['zt_money'] = $this->interest_records->whereIn('status', ['1', '2'])->sum('interest');

        switch ($this->investment_way) { // 投资状态  1常规  2通联付款
            case '1':
                $order['pay_type'] = '常规';
                break;
            case '2':
                $order['pay_type'] = '通联付款';
                break;
            default:
                $order['pay_type'] = '未知';
                break;
        }
        switch ($time_unit) { // 周期单位   1日、 2月、3年
            case '1':
                $order['product_longtime'] = $long_time . '日';
                break;
            case '2':
                $order['product_longtime']  = $long_time . '月';
                break;
            case '3':
                $order['product_longtime']  = $long_time . '年';
                break;
            default:
                $order['product_longtime']  = '';
                break;
        }
        switch ($pay_time_unit) {
            case '1':
                $order['product_pay_long'] = $pay_long . '日/次';
                break;
            case '2':
                $order['product_pay_long'] = $pay_long . '月/次';
                break;
            case '3':
                $order['product_pay_long'] = $pay_long . '年/次';
                break;
            default:
                $order['product_pay_long'] = '';
                break;
        }
        switch ($this->is_first) { // 是否为首个订单  1是新单2否存量 3转续
            case '1':
                $order['order_type'] = '新单';
                break;
            case '2':
                $order['order_type'] = '存量';
                break;
            case '3':
                $order['order_type'] = '转续';
                break;
            default:
                $order['order_type'] = '数据异常';
                break;
        }
        switch ($this->order_status) { // 订单状态 -1草稿， 1是待付款  2是审核中   3是待生效(弃用)  4是已生效 5是拒绝  6是已结束
            case '-1':
                $order['order_status_str'] = '草稿';
                break;
            case '1':
                $order['order_status_str'] = '待付款';
                break;
            case '2':
                switch ($this->audit_step) { // 是否为首个订单  1初审 2复审
                    case '1':
                        $order['order_status_str'] = '初审中';
                        break;
                    case '2':
                        $order['order_status_str'] = '复审中';
                        break;
                }
                break;
            case '3':
                $order['order_status_str'] = '待生效';
                break;
            case '4':
                $order['order_status_str'] = '已生效';
                break;
            case '5':
                $order['order_status_str'] = '拒绝';
                break;
            case '6':
                $order['order_status_str'] = '已完结';
                break;
            default:
                $order['order_status_str'] = '数据异常';
                break;
        }
        return $order;
    }
}
